/**
*
*/
package org.javacommerce.google;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;
import org.javacommerce.google.ws.API;
import org.jdom.Document;
import org.jdom.Element;
import com.google.checkout.CheckoutFlowSupport;
import com.google.checkout.CheckoutRedirect;
import com.google.checkout.CheckoutShoppingCart;
import com.google.checkout.Item;
import com.google.checkout.Items;
import com.google.checkout.MerchantCheckoutFlowSupport;
import com.google.checkout.ShoppingCart;
import com.google.checkout.UnitPrice;
/**
* @author Michael Blanton (mike@mikeblanton.com)
*/
public class APIUtil {
private static final String ELEM_REDIRECT_URL = "redirectURL";
private static final String ELEM_SIGNATURE = "signature";
private static final String ELEM_CART = "cart";
private static final String ELEM_CHECKOUT_SHOPPING_CART_DATA = "CheckoutShoppingCartData";
private static final String PARAM_EDIT_CART_URL = "editCartURL";
private static final String PARAM_CONTINUE_SHOPPING_URL = "continueShoppingURL";
private static final String PARAM_DOT_CURRENCY = ".currency";
private static final String USD = "USD";
private static final String PARAM_QUANTITY = "quantity_";
private static final String PARAM_UNIT_PRICE = "unit-price_";
private static final String PARAM_ITEM_DESCRIPTION = "item-description_";
private static final String PARAM_ITEM_NAME = "item-name_";
public static final String PARAM_REDIRECT = "redirect";
private static final Log LOG = LogFactory.getLog(APIUtil.class);
public static final CheckoutShoppingCart buildCheckoutShoppingCart(
HttpServletRequest _request) {
if (LOG.isDebugEnabled()) {
LOG.debug("Building CheckoutShoppingCart from HTTP Request");
}
CheckoutShoppingCart checkoutCart = new CheckoutShoppingCart();
ShoppingCart cart = new ShoppingCart();
Items items = new Items();
// Start with 0
int lineNum = 0;
while (_request.getParameter(PARAM_ITEM_NAME + lineNum) != null
&& _request.getParameter(PARAM_ITEM_DESCRIPTION + lineNum) != null
&& _request.getParameter(PARAM_UNIT_PRICE + lineNum) != null
&& _request.getParameter(PARAM_QUANTITY + lineNum) != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Found items for line " + lineNum);
}
Item item = new Item();
item.setItemName(_request.getParameter(PARAM_ITEM_NAME + lineNum));
item.setItemDescription(_request
.getParameter(PARAM_ITEM_DESCRIPTION + lineNum));
UnitPrice price = new UnitPrice();
price.setContent(new BigDecimal(_request
.getParameter(PARAM_UNIT_PRICE + lineNum)));
if (_request.getParameter(PARAM_UNIT_PRICE + lineNum + PARAM_DOT_CURRENCY) != null) {
price.setCurrency(_request.getParameter(PARAM_UNIT_PRICE
+ lineNum + PARAM_DOT_CURRENCY));
} else {
price.setCurrency(USD);
}
item.setUnitPrice(price);
item.setQuantity(Integer.parseInt(_request.getParameter(PARAM_QUANTITY
+ lineNum)));
items.addItem(item);
lineNum++;
}
cart.setItems(items);
if (LOG.isDebugEnabled()) {
LOG.debug("Added " + lineNum + " items to the cart.");
}
checkoutCart.setShoppingCart(cart);
CheckoutFlowSupport flowSupport = new CheckoutFlowSupport();
MerchantCheckoutFlowSupport mcfs = new MerchantCheckoutFlowSupport();
if (_request.getParameter(PARAM_CONTINUE_SHOPPING_URL) != null) {
mcfs.setContinueShoppingUrl(_request.getParameter(PARAM_CONTINUE_SHOPPING_URL));
}
if (_request.getParameter(PARAM_EDIT_CART_URL) != null) {
mcfs.setEditCartUrl(_request.getParameter(PARAM_EDIT_CART_URL));
}
flowSupport.setMerchantCheckoutFlowSupport(mcfs);
checkoutCart.setCheckoutFlowSupport(flowSupport);
return checkoutCart;
}
public static final Document generateXMLDocument(String _cartSig,
String _cart) {
Document doc = new Document();
Element root = new Element(ELEM_CHECKOUT_SHOPPING_CART_DATA);
root.addContent(new Element(ELEM_CART).setText(_cart));
root.addContent(new Element(ELEM_SIGNATURE).setText(_cartSig));
doc.setRootElement(root);
return doc;
}
public static final Document generateXMLDocument(String _cartSig,
String _cart, CheckoutRedirect _redirect) {
Document doc = generateXMLDocument(_cartSig, _cart);
doc.getRootElement().addContent(
new Element(ELEM_REDIRECT_URL, _redirect.getRedirectUrl()));
return doc;
}
public static final String generateCartSignature(CheckoutShoppingCart _cart)
throws InvalidKeyException, MarshalException, ValidationException,
NoSuchAlgorithmException {
String sig = API.generateCartSignature(_cart);
return API.base64Encode(sig);
// return Base64Coder.encode(sig);
}
public static final String encodeCart(CheckoutShoppingCart _cart)
throws MarshalException, ValidationException {
StringWriter writer = new StringWriter();
Marshaller.marshal(_cart, writer);
return API.base64Encode(writer.toString());
// return Base64Coder.encode(writer.toString());
}
}